package com.flca.pstc42.util;

import com.flca.pstc42.dto.AdresDto;


class CalcDistanceHelper {

//	private static final double EarthRadiusInMiles = 3956.0;
	private static final double EarthRadiusInKilometers = 6367.0;
	
	static double calcDistance(AdresDto adres1, AdresDto adres2)
	{
		return calcDistance(adres1.getLat(), adres1.getLng(), adres2.getLat(), adres2.getLng());
	}

	static double calcDistance(double lat1, double lng1, double lat2, double lng2)
	{
		return EarthRadiusInKilometers * 2 * Math.asin( Math.min(1, Math.sqrt( 
				( Math.pow(Math.sin((diffRadian(lat1, lat2)) / 2.0), 2.0) + 
						Math.cos(toRadian(lat1)) * Math.cos(toRadian(lat2)) * Math.pow(Math.sin((diffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
	}

	private static double toRadian(double v)
	{
		return v * (Math.PI / 180);
	}
	
	private static double diffRadian(double v1, double v2) 
	{
		return toRadian(v2) - toRadian(v1);
	}

}
